package top.abrsm.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import top.abrsm.config.LogPointer;
import top.abrsm.entity.CarEntity;
import top.abrsm.entity.FileEntity;
import top.abrsm.entity.ProductEntity;
import top.abrsm.entity.StudentEntity;
import top.abrsm.service.ICarService;
import top.abrsm.service.IFileService;
import top.abrsm.service.IProductService;
import top.abrsm.util.ConstUtil;
import top.abrsm.util.ResultBody;
import top.abrsm.util.StateEnum;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 * @author WuZH
 * @since 2023-11-18
 */
@Controller
@RequestMapping ("/car")
public class CarController {

    @Resource
    private ICarService iCarService;
    @Resource
    private IFileService iFileService;
    @Resource
    private IProductService iProductService;

    @RequestMapping ("queryList")
    public String queryList (CarEntity carEntity, HttpSession session, Model model) {
        StudentEntity studentEntity = (StudentEntity) session.getAttribute ( "studentEntity" );
        carEntity.setStuId ( studentEntity.getId () );
        List<CarEntity> carEntities = iCarService.queryList ( carEntity );
        List<ProductEntity> productEntities = new ArrayList<> ();
        double total = 0;
        for ( CarEntity entity : carEntities ) {
            ProductEntity productEntity = new ProductEntity ();
            productEntity.setId ( entity.getProductId () );
            productEntity.setCarId ( entity.getId () );
            productEntity.setProductName ( entity.getProductName () );
            productEntity.setActualPrice ( entity.getActualPrice () );
            // 查询加入购物车的商品
            FileEntity fileEntity = new FileEntity ();
            fileEntity.setFileJoinId ( productEntity.getId () );
            fileEntity.setFileJoinType ( 2 );
            List<FileEntity> fileEntities = iFileService.queryList ( fileEntity );
            productEntity.setFileEntities ( fileEntities );
            total += Double.parseDouble ( entity.getActualPrice () );
            productEntities.add ( productEntity );
        }
        model.addAttribute ( "productEntities", productEntities );
        model.addAttribute ( "totalPrice", total );
        return "car";
    }


    @RequestMapping ("saveSingle")
    @LogPointer ("新增购物车")
    public String saveSingle (@Param ("productId") String productId, HttpSession session, Model model) {
        StudentEntity studentEntity = (StudentEntity) session.getAttribute ( "studentEntity" );
        CarEntity carEntity = new CarEntity ();
        carEntity.setProductId ( productId );
        carEntity.setStuId ( studentEntity.getId () );
        carEntity = iCarService.saveSingle ( carEntity );
        ProductEntity productEntity = new ProductEntity ();
        productEntity.setId ( carEntity.getProductId () );
        productEntity = iProductService.querySingle ( productEntity );
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setFileJoinId ( productEntity.getId () );
        fileEntity.setFileJoinType ( 2 );
        List<FileEntity> fileEntities = iFileService.queryList ( fileEntity );
        productEntity.setFileEntities ( fileEntities );
        model.addAttribute ( "productEntities", productEntity );
        return "car";
    }

    @RequestMapping ("deleteSingle")
    public String deleteSingle (@Param ("id") String id) {
        CarEntity carEntity = new CarEntity ();
        carEntity.setId ( id );
        carEntity = iCarService.deleteSingle ( carEntity );
        return "redirect:/car/queryList";
    }

}
